// DSAddin.cpp : Implementation of CMouseGestureAddIn
#include "stdafx.h"
#include "MouseGestureAddIn.h"
#include "MouseGestureAddInAddin.h"
#include "MouseTracer.h"
#include "MSDevApp.h"

/////////////////////////////////////////////////////////////////////////////
// CMouseGestureAddIn

///////////////////////////////////////////////////////////////////////////////
// IDSAddin

STDMETHODIMP CMouseGestureAddIn::OnConnection(IApplication* piApplication, VARIANT_BOOL bFirstTime, long dwCookie, VARIANT_BOOL* pbOnConnection)
{
	// prepare to fail!
	*pbOnConnection = VARIANT_FALSE;

	// create our command object
	CCommandsObj::CreateInstance(&m_pCommands);
	m_pCommands->AddRef();
	m_pCommands->m_piApplication = piApplication;
	CComQIPtr<IDispatch> piDispCmds = m_pCommands;

	CMSDevApp::Instance().Attach(piApplication);

	// tell VC about us:
	if (FAILED(piApplication->SetAddInInfo((long)_Module.GetModuleInstance(), piDispCmds, IDR_TOOLBAR_MEDIUM, IDR_TOOLBAR_LARGE, dwCookie)))
		return S_OK;

	// register our commands with VC
	VARIANT_BOOL bRet;
	_bstr_t		 bstrCmd,
					 bstrMouseGestureToggle = "MouseGestureToggle",
					 bstrMouseGestureConfigure = "MouseGestureConfigure";


	// MouseGesutreToggle
	bstrCmd = "MouseGestureToggle\nMouseGestureToggle\nEnable/Disable Mouse Gesture\nMouseGestureToggle";
	if (FAILED(piApplication->AddCommand(bstrCmd, bstrMouseGestureToggle, 0, dwCookie, &bRet) || VARIANT_FALSE == bRet))
		return S_OK;

	// MouseGesutreConfigure
	bstrCmd = "MouseGestureConfigure\nMouseGestureConfigure\nConfigure Mouse Gesture\nMouseGestureConfigure";
	if (FAILED(piApplication->AddCommand(bstrCmd, bstrMouseGestureConfigure, 1, dwCookie, &bRet) || VARIANT_FALSE == bRet))
		return S_OK;

	// create toolbar:
	if (VARIANT_TRUE == bFirstTime)
	{
		piApplication->AddCommandBarButton(dsGlyph, bstrMouseGestureToggle, dwCookie);
		piApplication->AddCommandBarButton(dsGlyph, bstrMouseGestureConfigure, dwCookie);
	}

	// success
	*pbOnConnection = VARIANT_TRUE;

	// SECTION defined in stdafx.h
	// KEY## defined in MGConstant.h
	regbool bAutoloadingOnStartup(SECTION KEY15, HKEY_CURRENT_USER);
	if((BOOL)bAutoloadingOnStartup)
	{
		regbool bMouseGestureEnabled(SECTION KEY01, HKEY_CURRENT_USER);
		if((BOOL)bAutoloadingOnStartup)
		{
			MouseTracer::Setup();
		}
	}

	return S_OK;
}

STDMETHODIMP CMouseGestureAddIn::OnDisconnection(VARIANT_BOOL /*bLastTime*/)
{
	// cleanup!
	CMSDevApp::Instance().Detach();

	m_pCommands->Release();
	m_pCommands = NULL;

	return S_OK;
}
